
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;



import DAO.Iter.GiaoVienDAO;
import TableModel.Giaovien;
import java.io.IOException;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;


/**
 *
 * @author vt
 */
public class Login {
    public static final String SESSION_NAME_LOGIN ="sedkdfhaldsakfds12323";
    String[] danhsach ={"admin","giaovien","giaovu","sinhvien"};
    private GiaoVienDAO gvDao;
    private String userName;
    private String isLoged;

    public String getIsLoged() {
        return isLoged;
    }

    public void setIsLoged(String isLoged) {
        this.isLoged = isLoged;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
    private String password;
    public GiaoVienDAO getGvDao() {
        return gvDao;
    }

    @Override
    public String toString() {
        return "Login{" + "danhsach=" + danhsach + ", gvDao=" + gvDao + ", userName=" + userName + ", password=" + password + '}';
    }
    public void setGvDao(GiaoVienDAO gvDao) {
        this.gvDao = gvDao;
    }
    public String login(){
        System.out.println("Da Nhan Nut  login");
        System.out.println("User Name : " + userName + "  Pass :" + password);
        Giaovien ad = gvDao.findGiaoVien(userName, password);
      if(ad!= null){
            if(createSessionLogin(userName,"1")){
                 isLoged  ="Logout";
                return "mhshow?faces-redirect=true";
            }else{
                return "fail?faces-redirect=true";
            } 
        }else{
            return "fail?faces-redirect=true";
        }
    }
    public boolean createSessionLogin(String name,String value){
          Map m = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
         
          String a = (String) m.get(SESSION_NAME_LOGIN);
           if(a != null && a.equals("1")){
               return false;
           }else{
                HttpSession ses = ( HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(true);
		          ses.setAttribute(SESSION_NAME_LOGIN,value);
                          System.out.println((String)m.get(SESSION_NAME_LOGIN));
                         return true;
           }
    }
    public boolean checkLogin(String name){
              if(name.equals("1")){
                  return true;
              }
    
    return false;
}
   
    public String logout(){
        System.out.println("RUN... LOGOUT ");
        HttpSession ses = ( HttpSession )FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        if(ses != null){
                ses.setAttribute(SESSION_NAME_LOGIN,"0");
        }
        ses.invalidate();
        userName ="Login";
        return "logout";
    }
}